#!/bin/bash

# GitCode 问题反馈插件安装脚本
echo "🚀 GitCode 问题反馈插件安装脚本"
echo "=================================="

# 检查是否在 GitBook 项目中
if [ ! -f "book.json" ] && [ ! -f "book.js" ]; then
    echo "❌ 错误：请在 GitBook 项目根目录中运行此脚本"
    echo "💡 提示：确保当前目录包含 book.json 或 book.js 文件"
    exit 1
fi

# 创建插件目录
PLUGIN_DIR="node_modules/gitbook-plugin-gitcode-issue-feedback"
echo "📁 创建插件目录: $PLUGIN_DIR"
mkdir -p "$PLUGIN_DIR"

# 复制插件文件
echo "📦 复制插件文件..."
cp plugin-package/* "$PLUGIN_DIR/"

echo "✅ 插件文件复制完成"

# 检查 book.json 配置
if [ -f "book.json" ]; then
    echo "📝 检查 book.json 配置..."
    
    # 检查是否已包含插件
    if grep -q "gitcode-issue-feedback" book.json; then
        echo "✅ book.json 中已包含插件配置"
    else
        echo "⚠️  book.json 中未找到插件配置"
        echo ""
        echo "请手动在 book.json 中添加以下配置："
        echo ""
        echo "  \"plugins\": [\"gitcode-issue-feedback\"],"
        echo "  \"pluginsConfig\": {"
        echo "    \"gitcode-issue-feedback\": {"
        echo "      \"repo\": \"your-username/your-repo\","
        echo "      \"label\": \"报告问题\","
        echo "      \"private\": false"
        echo "    }"
        echo "  }"
        echo ""
    fi
elif [ -f "book.js" ]; then
    echo "✅ 检测到 book.js 文件"
    echo "请确保 book.js 中包含插件配置"
fi

echo ""
echo "🎉 安装完成！"
echo ""
echo "📋 下一步："
echo "1. 检查并更新 book.json 或 book.js 中的插件配置"
echo "2. 运行 'gitbook serve' 启动服务"
echo "3. 在浏览器中测试插件功能"
echo ""
echo "🔧 配置示例："
echo "  \"plugins\": [\"gitcode-issue-feedback\"],"
echo "  \"pluginsConfig\": {"
echo "    \"gitcode-issue-feedback\": {"
echo "      \"repo\": \"your-username/your-repo\","
echo "      \"label\": \"报告问题\","
echo "      \"private\": false"
echo "    }"
echo "  }"
